home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / asm / cm_int.exe / CM-MACS.LIB < prev    next >
Encoding:
Text File  |  1993-04-10  |  21.4 KB  |  654 lines

  1.  
  2. ;   CM-MACS.LIB  -  By CFMartin  -  2/15/90
  3. ;
  4. ;   A Collection of Standard Assembly Macros written for use with the
  5. ;   Wolfware Assembler WASM, and the interrupt service provided by CM-INT.COM
  6. ;
  7. ;   General Notes:
  8. ;       1.  These macros are not necessarily compatible with any assembler
  9. ;           except WASM.
  10. ;       2.  Arguments for these macros are of two types, denoted by the
  11. ;           suffixes "_lab" and "_imm", for "label" and "immediate,"  based
  12. ;           on the expected most frequent usage.
  13. ;               a.  _lab indicates that the macro is looking for a
  14. ;                   label for a memory operand in the data segment.
  15. ;               b.  _imm indicates that the macro is looking for an immediate
  16. ;                   operand. To reference memory in the data segment, just
  17. ;                   enclose the operand label in brackets.
  18. ;       3.  Names and syntax of some macros will look very familiar to
  19. ;           C programmers, as they are patterned after standard C functions
  20.  
  21. ;   VIDEO/KEYBOARD MANAGEMENT
  22.  
  23. ;------------------
  24. vpage       macro   vpage_imm    ;selects video page
  25. ;------------------
  26. ;   vpage_imm   byte    video page number to be selected
  27. ;------------------
  28.             mov     ah,5
  29.             mov     al,vpage_imm
  30.             int     10h
  31.             endm
  32.  
  33. ;------------------
  34. vmode       macro   vmode_imm    ;sets video mode
  35. ;------------------
  36. ;   vmode_num   byte    video mode number
  37. ;------------------
  38.             mov     ah,0
  39.             mov     al,vmode_imm
  40.             int     10h
  41.             endm
  42.  
  43. ;------------------
  44. poscurs  macro row_imm,column_imm  ;positions cursor on active page (mode 2,3)
  45. ;------------------
  46. ;   row_imm,column_imm  bytes   0,0=upper left corner
  47. ;------------------
  48.         mov     ah,0Fh          ;get video state
  49.         int     10h
  50.         mov     ah,2            ;service 2 -- position cursor
  51.         mov     dh,row_imm      ;bh contains page
  52.         mov     dl,column_imm
  53.         int     10h             ;set cursor
  54.         endm
  55.  
  56. ;------------------
  57. clrscrn     macro   color_imm   ;clears active screen page (mode 2,3)
  58. ;------------------
  59. ;   color_imm       byte    attribute to clear screen with
  60. ;------------------
  61.         mov     ah,0Fh      ;get video state
  62.         int     10h
  63.         mov     al,bh       ;page
  64.         mov     ah,0
  65.         mov     bx,100h     ;256 paras per page modes 2 & 3
  66.         mul     bx
  67.         add     ax,0B800h   ;seg of page 0
  68.         mov     es,ax
  69.         mov dl,color_imm
  70.         mov di,0
  71.         mov ah,dl
  72.         mov cx,2000
  73.         mov al,20h      ;blank
  74.         cld
  75.         rep
  76.         stosw
  77.         endm
  78.  
  79. ;------------------
  80. pause    macro   key_name_imm   ;pauses until the named key is pressed
  81. ;------------------
  82. ;   key_name_imm    byte    key to wait for
  83. ;------------------
  84.         mov     bl,byte key_name_imm
  85.         mov     bh,00h
  86.         int     0C0h
  87.         endm
  88.  
  89.  
  90. ;------------------
  91. print   macro   str_lab     ;print string at current cursor pos on active page
  92. ;------------------
  93. ;   str_lab     byte        label of ASCIIZ string to print
  94. ;------------------
  95.         lea     si,str_lab
  96.         mov     bh,01h
  97.         int     0C0h
  98.         endm
  99.  
  100. ;------------------
  101. input   macro   str_lab   ;input string to <Rtn> with echo to active page
  102. ;------------------
  103. ;   str_lab     byte        label of ASCIIZ destination string
  104. ;------------------
  105.             lea     si,str_lab
  106.             mov     bh,02h
  107.             int     0C0h
  108.             endm
  109.  
  110. ;------------------
  111. scan    macro   prompt_lab,data_lab,data_len_imm
  112. ;------------------
  113. ;   prompt_lab      byte        label of ASCIIZ prompt string
  114. ;   data_lab        byte        label of ASCIIZ dest string for input
  115. ;   data_len_imm    byte        unsigned length of dest string
  116. ;   (Use no filtering, no blank stripping, window color 0Fh)
  117. ;------------------
  118.         lea     si,prompt_lab
  119.         lea     di,data_lab
  120.         mov     al,data_len_imm
  121.         mov     ah,0        ;no input data filtering
  122.         mov     cl,0        ;no blank stripping
  123.         mov     ch,0Fh      ;window color=hi intens wh, blk bkgd
  124.         mov     bx,300h     ;upper and lower case accepted
  125.         int     0C0h
  126.         endm
  127.  
  128. ;------------------
  129. lprint macro str_lab
  130. ;------------------
  131. ; Prints ASCIIZ string at ds:offset str_lab to parallel printer
  132. ;------------------
  133.  
  134.  mov cx,-1
  135.  lea si,str_lab
  136.  mov bh,4
  137.  int 0C0h
  138.  endm
  139.  
  140. ;------------------
  141. lprintc macro ctrl_str_lab
  142. ;------------------
  143. ; Prints printer control string at ds:offset ctrl_str_lab to parallel printer
  144. ; First byte of control string is length; remainder is string
  145. ;------------------
  146.  
  147.  mov cl,[ctrl_str_lab]
  148.  xor ch,ch
  149.  lea si,ctrl_str_lab
  150.  inc si
  151.  mov bh,4
  152.  int 0C0h
  153.  endm
  154.  
  155.  
  156.  
  157. ;   NUMERICAL CONVERSION
  158.  
  159. ;------------------
  160. ltoa    macro   dw_lab,str_lab  ;convert 32-bit number to decimal ASCIIZ string
  161. ;------------------
  162. ;   dw_lab      double word     32-bit number to be converted
  163. ;   str_lab     byte            label of ASCIIZ dest string in ds
  164. ;------------------
  165.             lea     si,str_lab
  166.             mov     ax,[offset dw_lab]      ;low order 16 bits
  167.             mov     dx,[offset dw_lab+2]    ;high order 16 bits,
  168.                                             ;including sign (bit 31)
  169.             mov     bl,0                    ;decimal return
  170.             mov     bh,06h
  171.             int     0C0h
  172.             endm
  173.  
  174. ;------------------
  175. ltoa2   macro   dw_lab,str_lab,base_imm     ;convert with base option
  176. ;------------------
  177. ;   dw_lab      double word     32-bit number to be converted
  178. ;   str_lab     byte            label of ASCIIZ dest string in ds
  179. ;   base_imm    byte            dec=0,hex=1,oct=2,bin=3
  180. ;------------------
  181.             lea     si,str_lab
  182.             mov     ax,[offset dw_lab]    ;low order 16 bits
  183.             mov     dx,[offset dw_lab+2]  ;high order 16 bits,
  184.                                             ;including sign (bit 31)
  185.             mov     bl,base_imm
  186.             mov     bh,06h
  187.             int     0C0h
  188.             endm
  189. ;------------------
  190. atol    macro   dw_lab,str_lab  ;conv string to 32-bit d-word
  191. ;------------------
  192. ;   dw_lab          double word     32-bit number to be converted
  193. ;   str_lab         byte            label of ASCIIZ source string in ds
  194. ;------------------
  195.             lea     si,str_lab
  196.             mov     bh,07h
  197.             int     0C0h
  198.             mov     [offset dw_lab],ax    ;low order 16-bits
  199.             mov     [offset dw_lab+2],dx  ;high order 16-bits
  200.             endm
  201.  
  202. ;------------------
  203. itoa    macro   w_lab,str_lab       ;conv signed 16-bit word to dec string
  204. ;------------------
  205. ;   w_lab           word        signed 16-bit number to be converted
  206. ;   str_lab         byte        label of ASCIIZ dest string in ds
  207. ;------------------
  208.             lea     si,str_lab
  209.             mov     ax,[offset w_lab] ;16-bit signed no.
  210.             cwd                     ;extend to d-word
  211.             mov     bl,0            ;return decimal
  212.             mov     bh,06h
  213.             int     0C0h
  214.             endm
  215.  
  216. ;------------------
  217. itoa2   macro   w_lab,str_lab,base_imm  ;conv signed 16-bit word to string
  218. ;------------------
  219. ;   w_lab           word        signed 16-bit number to be converted
  220. ;   str_lab         byte        label of ASCIIZ dest string in ds
  221. ;   base_imm        byte        dec=0,hex=1,oct=2,bin=3
  222. ;------------------
  223.             lea     si,str_lab
  224.             mov     ax,[offset w_lab] ;16-bit signed no.
  225.             cwd                     ;extend to d-word
  226.             mov     bl,base_imm
  227.             mov     bh,06h
  228.             int     0C0h
  229.             endm
  230. ;------------------
  231. atoi    macro   w_lab,str_lab   ;conv string to signed 16-bit
  232. ;------------------
  233. ;   w_lab           word        signed 16-bit number to be converted
  234. ;   str_lab         byte        label of ASCIIZ source string in ds
  235. ;------------------
  236.             lea     si,str_lab
  237.             mov     bh,07h
  238.             int     0C0h
  239.             mov     [offset w_lab],ax   ;if -32768 > i > 32787, lose
  240.                                         ;the significant bits
  241.             endm
  242.  
  243. ;------------------
  244. utoa    macro   w_lab,str_lab   ;conv unsigned 16-bit to dec string
  245. ;------------------
  246. ;   w_lab           word        unsigned 16-bit number to be converted
  247. ;   str_lab         byte        label of ASCIIZ dest string in ds
  248. ;------------------
  249.             lea     si,str_lab
  250.             mov     ax,[offset w_lab]
  251.             mov     dx,0
  252.             mov     bl,0            ;return decimal string
  253.             mov     bh,06h
  254.             int     0C0h
  255.             endm
  256.  
  257. ;------------------
  258. utoa2   macro   w_lab,str_lab,base_imm  ;conv unsigned 16-bit to string
  259. ;------------------
  260. ;   w_lab           word        signed 16-bit number to be converted
  261. ;   str_lab         byte        label of ASCIIZ dest string in ds
  262. ;   base_imm        byte        dec=0,hex=1,oct=2,bin=3
  263. ;------------------
  264.             lea     si,str_lab
  265.             mov     ax,[offset w_lab]
  266.             mov     dx,0
  267.             mov     bl,base_imm
  268.             mov     bh,06h
  269.             int     0C0h
  270.             endm
  271. ;------------------
  272. atou    macro   w_lab,str_lab   ;conv string to unsigned 16-bit
  273. ;------------------
  274. ;   w_lab           word        unsigned 16-bit number to be converted
  275. ;   str_lab         byte        label of ASCIIZ source string in ds
  276. ;------------------
  277.             lea     si,str_lab
  278.             mov     bh,07h
  279.             int     0C0h
  280.             mov     [offset w_lab],ax ;if i>65535, lose the significant bits
  281.             endm
  282.  
  283. ;   DATE/TIME CONVERSION
  284.  
  285. ;------------------
  286. caltojul    macro   jul_lab,cal_lab     ;converts calendar string to julian
  287. ;------------------
  288. ;   jul_lab         word    julian date destination
  289. ;   cal_lab         byte    label of ASCIIZ source string date in ds
  290. ;   (Century prefix is returned in bl)
  291. ;------------------
  292.             lea     si,cal_lab
  293.             mov     bh,11h
  294.             int     0C0h
  295.             mov     [offset jul_lab],ax
  296.                                     ;NOTE: If the calendar string
  297.                                     ;date has a 2-digit yr, 19 is assumed as
  298.                                     ;the century prefix
  299.             endm
  300.  
  301. ;------------------
  302. jultocal    macro   jul_lab,cal_lab     ;converts julian to calendar string
  303. ;------------------
  304. ;   jul_lab         word    julian date destination
  305. ;   cal_lab         byte    label of ASCIIZ source string date in ds
  306. ;------------------
  307.             lea     si,cal_lab
  308.             mov     bh,10h
  309.             mov     bl,19   ;all dates are assumed to have cent pref 19,
  310.                             ;and the display will have 2 digits in the year
  311.                             ;If 4 digits are desired, or date other than in
  312.                             ;century prefix 19, use jultocal4 below
  313.             mov     ax,[offset jul_lab]
  314.             mov     cx,2    ;two digit display for year
  315.             int     0C0h
  316.             endm
  317.  
  318. ;------------------
  319. jultocal4   macro   century_lab,jul_lab,cal_lab ;4-digit year display
  320. ;------------------
  321. ;   century_lab     byte    century prefix (eg, 21 for the year 2167)
  322. ;   jul_lab         word    julian date destination
  323. ;   cal_lab         byte    label of ASCIIZ source string date in ds
  324. ;------------------
  325.             lea     si,cal_lab
  326.             mov     bh,10h
  327.             mov     bl,[century_lab]
  328.             mov     ax,[jul_lab]
  329.             mov     cx,4    ;display 4 digits of year
  330.             int     0C0h
  331.             endm
  332.  
  333. ;------------------
  334. getdate     macro   cal_lab   ;puts system date string into cal_lab
  335. ;------------------
  336. ;   cal_lab         byte    label of ASCIIZ source string date in ds
  337. ;------------------
  338.             lea     si,cal_lab
  339.             mov     bh,12h
  340.             mov     cx,2    ;two-digit year display (assumption is
  341.                             ;century prefix=19)
  342.             int     0C0h
  343.             endm
  344.  
  345. ;------------------
  346. setdate     macro   cal_lab   ;sets system date to string in cal_lab
  347. ;------------------
  348. ;   cal_lab         byte    label of ASCIIZ source string date in ds
  349. ;------------------
  350.             lea     si,cal_lab
  351.             mov     bh,13h
  352.             int     0C0h
  353.             endm
  354.  
  355. ;------------------
  356. timetostr   macro   time_lab,str_lab
  357. ;------------------
  358. ;   time_lab    double word     number of seconds since the previous midnight
  359. ;   str_lab     byte            label of ASCIIZ string time format hh:mm:ss
  360. ;------------------
  361.             lea     si,str_lab
  362.             mov     ax,[time_lab]
  363.             mov     dx,[time_lab + 2]
  364.             mov     bh,15h
  365.             int     0C0h
  366.             endm
  367.  
  368. ;------------------
  369. strtotime   macro   time_lab,str_lab
  370. ;------------------
  371. ;   time_lab    double word     number of seconds since the previous midnight
  372. ;   str_lab     byte            label of ASCIIZ string time format hh:mm:ss
  373. ;------------------
  374.             lea     si,str_lab
  375.             mov     bh,16h
  376.             int     0C0h
  377.             mov     [time_lab],ax
  378.             mov     [time_lab + 2],dx
  379.             endm
  380.  
  381. ;------------------
  382. gettime     macro   time_lab,str_lab    ;gets current time from system
  383. ;------------------
  384. ;   time_lab    double word     number of seconds since the previous midnight
  385. ;   str_lab     byte            label of ASCIIZ string time format hh:mm:ss
  386. ;------------------
  387.             lea     si,str_lab
  388.             mov     bh,17h
  389.             int     0C0h
  390.             mov     [time_lab],ax
  391.             mov     [time_lab + 2],dx
  392.             endm
  393.  
  394. ;------------------
  395. settime     macro   str_lab   ;sets system time
  396. ;------------------
  397. ;   str_lab     byte            label of ASCIIZ string time format hh:mm:ss
  398. ;------------------
  399.             lea     si,str_lab
  400.             mov     bh,18h
  401.             int     0C0h
  402.             endm
  403.  
  404. ;------------------
  405. timer       macro
  406. ;------------------
  407. ;   Returns:
  408. ;   dx:ax           double word     number of seconds since last midnite
  409. ;   bx              word            number of seconds elapsed since last call
  410. ;------------------
  411.             mov     bh,19h
  412.             int     0C0h
  413.             endm
  414.  
  415. ;   STRING MANAGEMENT
  416.  
  417. ;------------------
  418. strcpy      macro   dest_lab,source_lab
  419. ;------------------
  420. ;   dest_lab        byte        label of ASCIIZ dest string in ds
  421. ;   source_lab      byte        label of ASCIIZ source string in ds
  422. ;------------------
  423.             lea     si,source_lab
  424.             lea     di,dest_lab
  425.             push    ds
  426.             pop     es
  427.             mov     bh,20h
  428.             int     0C0h
  429.             endm
  430.  
  431. ;------------------
  432. strlen      macro   string_lab
  433. ;------------------
  434. ;   string_lab      byte        label of ASCIIZ source string in ds
  435. ;   Returns:
  436. ;   ax              word        length of string
  437. ;------------------
  438.             lea     si,string_lab
  439.             mov     bh,21h
  440.             int     0C0h
  441.             endm            ;str length is in ax
  442.  
  443. ;------------------
  444. strcat      macro   dest_lab,source_lab
  445. ;------------------
  446. ;   dest_lab        byte        label of ASCIIZ dest string in ds
  447. ;   source_lab      byte        label of ASCIIZ source string in ds
  448. ;------------------
  449.             lea     di,dest_lab
  450.             lea     si,source_lab
  451.             mov     bh,22h
  452.             int     0C0h
  453.             endm
  454.  
  455. ;------------------
  456. strcmp      macro   str_1_lab,str_2_lab,len_imm
  457. ;------------------
  458. ;   str_1_lab,str_2_lab     bytes       labels in ds of ASCIIZ strings
  459. ;   len_imm         word    maximum length of comparison
  460. ;   Compares str1 to str2. Returns:
  461. ;       ax=0 if strings are equal, ax=1 if str1>str2, ax=-1 if str1<str2
  462. ;       bx=position number of first unequal byte (first position is 0)
  463. ;------------------
  464.             lea     si,str_1_lab
  465.             lea     di,str_2_lab
  466.             mov     cx,len_imm
  467.             mov     bh,23h
  468.             int     0C0h
  469.             endm
  470.  
  471. ;------------------
  472. strswap     macro   str_1_lab,str_2_lab,len_imm
  473. ;------------------
  474. ;   str_1_lab,str_2_lab     bytes       labels in ds of ASCIIZ strings
  475. ;   len_imm                 word        number of bytes to swap
  476. ;------------------
  477.             lea     si,str_1_lab
  478.             lea     di,str_2_lab
  479.             mov     cx,len_imm
  480.             mov     bh,24h
  481.             int     0C0h
  482.             endm
  483.  
  484. ;------------------
  485. strimbed    macro   dest_lab,source_lab
  486. ;------------------
  487. ;   dest_lab,source_lab     bytes       labels in ds of ASCIIZ strings
  488. ;       Imbeds (copies without terminal NULL) source into destination
  489. ;------------------
  490.             lea     si,source_lab
  491.             lea     di,dest_lab
  492.             mov     bh,25h
  493.             int     0C0h
  494.             endm
  495.  
  496. ;------------------
  497. allcaps     macro   string_lab
  498. ;------------------
  499. ;   string_lab          byte        label in ds of ASCIIZ string
  500. ;       Capitalizes all lower case letters in string at string_lab
  501. ;------------------
  502.             lea     si,string_lab
  503.             mov     cx,0    ;max of 32768 chars
  504.             mov     bh,26h
  505.             int     0C0h
  506.             endm
  507.  
  508.  
  509. ;   MEMORY MANAGEMENT
  510.  
  511. ;------------------
  512. dealloc     macro
  513. ;------------------
  514. ;   Deallocates all memory excess to immediate code needs + 512 bytes for
  515. ;   stack.
  516. ;------------------
  517. STACK_BYTES equ     200h        ;stack to equal 512 bytes
  518.             mov     ax,$SIZE
  519.             add     ax,100h     ;for PSP
  520.             add     ax,STACK_BYTES
  521.             mov     sp,ax
  522.  
  523.             mov     cl,4
  524.             shr     ax,cl
  525.             inc     ax          ;1 para buffer
  526.             mov     bx,ax
  527.             push    cs
  528.             pop     es
  529.             mov     ah,4Ah
  530.             int     21h         ;memory reallocated
  531.             endm
  532.  
  533. ;------------------
  534. alloc       macro   no_paras_imm,memseg_lab
  535. ;------------------
  536. ;   Allocates no_paras_imm paragraphs, puts memory segment in memseg_lab.
  537. ;   If asking for a lot, check carry flag for error
  538. ;       memseg_lab      = word label
  539. ;------------------
  540.             mov     bx,no_paras_imm
  541.             mov     ah,48h
  542.             int     21h
  543.             mov     [memseg_lab],ax
  544.             endm
  545.  
  546. ;------------------
  547. alloc_all   macro   no_paras_lab,memseg_lab
  548. ;------------------
  549. ;   Allocates all available memory, puts memory segment in memseg_lab, and
  550. ;   number of paragraphs available in no_paras_lab.
  551. ;       memseg_lab,no_paras_lab = word labels
  552. ;------------------
  553.             mov     bx,0FFFFh   ;ask for impossible
  554.             mov     ah,48h
  555.             int     21h
  556.             mov     ah,48h
  557.             mov     [no_paras_lab],bx
  558.             int     21h
  559.             mov     [memseg_lab],ax
  560.             endm
  561.  
  562. ;------------------
  563. freemem    macro   memseg_lab
  564. ;------------------
  565. ;   Frees the block of memory identified by the segment stored in memseg_lab.
  566. ;       memseg_lab      = word label
  567. ;------------------
  568.             mov     es,[memseg_lab]
  569.             mov     ah,49h
  570.             int     21h
  571.             endm
  572.  
  573. ;------------------
  574. xmemput     macro   dest_seg_lab,dest_off_lab,source_name_lab,num_imm
  575. ;------------------
  576. ;   Put string into extended memory from data segment
  577. ;   dest_seg_lab    word in memory containing segment of destination block
  578. ;   dest_off_lab    word in memory containing offset of destination block
  579. ;   source_name_lab byte            label of source block in ds
  580. ;   num_imm         word            number of bytes to move
  581. ;------------------
  582.             mov     ax,[dest_seg_lab]
  583.             mov     es,ax
  584.             mov     di,[dest_off_lab]
  585.             lea     si,source_name_lab
  586.             mov     cx,num_imm
  587.             cld
  588.             rep
  589.             movsb
  590.             endm
  591.  
  592. ;------------------
  593. xmemget      macro   dest_name_lab,source_seg_lab,source_off_lab,num_imm
  594. ;------------------
  595. ;   Get string from extended memory into data segment
  596. ;   dest_name_lab   byte        label of destination block in ds
  597. ;   source_seg_lab  word in memory containing segment of source block
  598. ;   source_off_lab  word in memory containing offset of source block
  599. ;   num_imm         word        number of bytes to move
  600. ;------------------
  601.             push    ds
  602.             mov     es,ds
  603.             lea     di,dest_name_lab
  604.             mov     si,[source_off_lab]
  605.             mov     ax,[source_seg_lab]
  606.             mov     ds,ax
  607.             mov     cx,num_imm
  608.             cld
  609.             rep
  610.             movsb
  611.             pop     ds
  612.             endm
  613.  
  614. ;   MISCELLANEOUS
  615.  
  616. ;------------------
  617. exit    macro   code_imm       ;exit code for calls by DOS errorlevel
  618. ;------------------
  619. ;   code_imm    byte            code for parent or DOS use
  620. ;------------------
  621.         mov     al,code_imm
  622.         mov     ah,4ch
  623.         int     21h
  624.         endm
  625.  
  626. ;------------------
  627. pushall     macro
  628. ;------------------
  629.             push    bp
  630.             push    ax
  631.             push    bx
  632.             push    cx
  633.             push    dx
  634.             push    ds
  635.             push    es
  636.             push    si
  637.             push    di
  638.             endm
  639.  
  640. ;------------------
  641. popall      macro
  642. ;------------------
  643.             pop     di
  644.             pop     si
  645.             pop     es
  646.             pop     ds
  647.             pop     dx
  648.             pop     cx
  649.             pop     bx
  650.             pop     ax
  651.             pop     bp
  652.             endm
  653.  
  654.